﻿using System;
using System.Configuration;
using ReportWizard.Properties;

namespace ReportWizard.Helpers
{
    public class ConfigReader
    {
        private static void FailAction(string errorMessage, bool exitOnError, bool showError)
        {
            if (showError && !string.IsNullOrEmpty(errorMessage))
            {
                ShowMessage.Error(errorMessage);
            }

            if (exitOnError)
            {
                ProcessManager.ExitOnError(errorMessage);
            }
        }

        private static string GetConnectionString(string connectionString, string errorMessage, bool exitOnError)
        {
            try
            {
                string readedConnectionString = ConfigurationManager.ConnectionStrings

[connectionString].ConnectionString;
                if (!string.IsNullOrEmpty(readedConnectionString))
                {
                    return Crypto.Decrypt(Convert.FromBase64String(readedConnectionString));
                }
                else
                {
                    FailAction(errorMessage, exitOnError, true);
                    return null;
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(errorMessage);
                Logger.WriteLog(ex);
                return null;
            }
        }

        private static string GetAppSettings(string settingName, string errorMessage, bool exitOnError, bool showError, bool encrypted)
        {
            try
            {
                string readedSettingName = ConfigurationManager.AppSettings[settingName];
                if (!string.IsNullOrEmpty(readedSettingName))
                {
                    return encrypted ? Crypto.Decrypt(Convert.FromBase64String(readedSettingName)) :

readedSettingName;
                }
                else
                {
                    FailAction(errorMessage, exitOnError, showError);
                    return null;
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(errorMessage);
                Logger.WriteLog(ex);
                return null;
            }
        }

        public static string OilWizardConectionString
        {
            get
            {
                return string.Format(GetConnectionString("DefaultServerString", Resources.FailedConnectionToDataBase, true), ServerAddress, ServerPort, DbName, DbUserLogin, DbUserPassword);
            }
        }

        public static string DefaultServerString
        {
            get
            {
                return string.Format(GetConnectionString("TrustedServerString", Resources.FailedConnectionToDataBaseServer, true), ServerAddress, ServerPort);
            }
        }

        private static string ServerAddress
        {
            get { return GetAppSettings("ServerAddress", Resources.FailedConnectionToDataBase, true, true, false); }
        }

        private static string ServerPort
        {
            get { return GetAppSettings("ServerPort", Resources.FailedConnectionToDataBase, true, true, false); }
        }

        public static string DefaultUser
        {
            get { return GetAppSettings("DefaultUser", Resources.ErrorCreatingDefaultUser, true, true, true); }
        }

        public static string DbParent
        {
            get
            {
                return GetAppSettings("DbParent", Resources.FailedGetDataBaseName, true, true, true);
            }
        }

        public static string DbName
        {
            get
            {
                return GetAppSettings("DbName", Resources.FailedGetDataBaseName, true, true, true);
            }
        }

        public static string DbUserLogin
        {
            get
            {
                return GetAppSettings("Ul", Resources.FailedConnectionToDataBase, false, false, true);
            }
        }

        public static string DbUserPassword
        {
            get
            {
                return GetAppSettings("Up", Resources.FailedConnectionToDataBase, false, false, true);
            }
        }

        public static string SmtpServer
        {
            get
            {
                return GetAppSettings("SmtpServer", Resources.SmtpFailedGetServer, false, false, true);
            }
        }

        public static string SmtpLogin
        {
            get
            {
                return GetAppSettings("SmtpLogin", Resources.SmtpFailedGetLogin, false, false, true);
            }
        }

        public static string SmtpPassword
        {
            get
            {
                return GetAppSettings("SmtpPassword", Resources.SmtpFailedGetPassword, false, false, true);
            }
        }

        public static string DeveloperMail
        {
            get
            {
                return GetAppSettings("DeveloperMail", Resources.SmtpFailedGetDeveloperMail, false, false, true);
            }
        }

        public static string PingServer
        {
            get
            {
                return GetAppSettings("PingServer", string.Empty, false, false, false);
            }
        }

        public static int PingCount
        {
            get
            {
                return Convert.ToInt32(GetAppSettings("PingCount", string.Empty, false, false, false));
            }
        }

        public static int PingTimeout
        {
            get
            {
                return Convert.ToInt32(GetAppSettings("PingTimeout", string.Empty, false, false, false));
            }
        }
    }
}